package com.rykj.qxj.rest;


import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.common.exception.*;
import com.rykj.qxj.server.model.entity.*;
import com.rykj.qxj.server.model.param.ApiEnableParams;
import com.rykj.qxj.server.model.param.ApiManagerOutsideUrlParams;
import com.rykj.qxj.server.model.param.ApiManagerParams;
import com.rykj.qxj.server.services.*;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.RegexUtils;
import com.rykj.qxj.util.Result;
import com.techindata.cacheredis.CacheZSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import net.sf.jsqlparser.JSQLParserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j
@RestController
@RequestMapping("/v1/apiManage")
@Api(tags="接口管理")
public class ApiManageController {
	
	@Autowired
	private IApiManageService apiManageService;
	
	@Autowired
	private IApiVersionManageService apiVersionManageService;
	
	@Autowired
	private IApiRequestParamService apiRequestParamService;
	
	@Autowired
	private IApiResponseParamService apiResponseParamService;
	
	@Autowired
	private IAddFieldService  addFieldService;
	
	@Autowired
	private IUpdateFieldService updateFieldService;
	
	private static final String PER_API_VISITCOUNT= "perApiVisitCount";

	@PostMapping("/save")
    @ApiOperation(value="开发者中心保存接口",notes ="开发者中心保存接口")
	@EnableSystemLog(value="开发者中心保存接口",fullPath ="/v1/apiManage/save")
    public Result saveApiManage(@RequestBody ApiManagerParams apiManagerParams){
		if(null==apiManagerParams) {
			return Result.failure("数据格式错误,请检查");
		}
		try {
			apiManageService.saveApiManage(apiManagerParams);
		} catch (ApiManageSaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false,e.getMessage());
		}
		catch (ApiVersionManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableOfFieldIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableSaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiRequestParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiResponseParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApplyIdIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (RequestParamAndUpdateFieldDuplicatePenNameExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (JSQLParserException e) {
			e.printStackTrace();
			return new Result(false, "Sql语句语法不对,请检查!");
		} catch (SqlTempletIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false,e.getMessage());
		} catch (ApiRequestParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		  return new Result(true,"保存成功");
    }
	@PostMapping("/adminSave")
	@ApiOperation(value="接口管理保存接口",notes ="接口管理保存接口")
	@EnableSystemLog(value="后台管理员保存接口",fullPath ="/v1/apiManage/adminSave")
	public Result adminSaveApiManage(@RequestBody ApiManagerParams apiManagerParams){
		if(null==apiManagerParams) {
			return Result.failure("数据格式错误,请检查");
		}
		try {
			apiManageService.adminSaveApiManage(apiManagerParams);
		} catch (ApiManageSaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false,e.getMessage());
		}
		catch (ApiVersionManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableOfFieldIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiOfTableSaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiRequestParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiResponseParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApplyIdIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (RequestParamAndUpdateFieldDuplicatePenNameExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (JSQLParserException e) {
			e.printStackTrace();
			return new Result(false,  "Sql语句语法不对,请检查!");
		} catch (SqlTempletIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (FileApiCatalogListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (FileApiNameListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (NameByUrlIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (NameByUrlIsDupExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		return new Result(true,"保存成功");
	}
	@PostMapping("/saveOutsideUrl")
    @ApiOperation(value="接口管理第三方接口保存",notes ="接口管理第三方接口保存")
	@EnableSystemLog(value="接口管理第三方接口保存",fullPath ="/v1/apiManage/saveOutsideUrl")
	public Result saveApiManageOutsideUrl(@RequestBody ApiManagerOutsideUrlParams apiManagerOutsideUrlParams){
		if(null==apiManagerOutsideUrlParams) {
			return Result.failure("数据格式错误,请检查");
		}
		try {
			apiManageService.saveApiManageOutsideUrl(apiManagerOutsideUrlParams);
		} catch (ApiManageSaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false,e.getMessage());
		}
		catch (ApiVersionManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		catch (ApiManageIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApplyIdIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		return new Result(true,"保存成功");
	}
	@PutMapping("/updateOutsideUrl")
	@ApiOperation(value="第三方接口更新",notes ="第三方接口更新")
	@EnableSystemLog(value="第三方接口更新",fullPath ="/v1/apiManage/updateOutsideUrl")
	public Result updateApiManageOutsideUrl(@RequestBody ApiManagerOutsideUrlParams apiManagerOutsideUrlParams){
		if(null==apiManagerOutsideUrlParams) {
			return Result.failure("数据格式错误,请检查");
		}
		try {
			apiManageService.updateApiManageOutsideUrl(apiManagerOutsideUrlParams);
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Result(true,"修改成功");
	}
	@PutMapping("/adminUpdate")
    @ApiOperation(value="管理员-接口管理更新接口信息",notes ="管理员-接口管理更新接口信息")
	@EnableSystemLog(value="管理员更新接口",fullPath ="/v1/apiManage/adminUpdate")
    public Result adminUpdateApiManage(@RequestBody ApiManagerParams apiManagerParams){
		if(null==apiManagerParams) {
			return Result.failure("数据格式错误,请检查");
		}
        try {
			apiManageService.adminUpdateApiManage(apiManagerParams);
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiOfTableListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiOfTableOfFieldIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (RequestParamAndUpdateFieldDuplicatePenNameExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (SqlTempletIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (JSQLParserException e) {
			e.printStackTrace();
			return new Result(false, "Sql语句语法不对,请检查!");
		} catch (ApiRequestParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"缓存中接口基本信息删除失败!");
		}
        	return new Result(true,"更新成功");
    }
	@PutMapping("/update")
	@ApiOperation(value="开发者中心-接口管理更新接口信息",notes ="开发者中心-接口管理更新接口信息")
	@EnableSystemLog(value="开发者中心更新接口",fullPath ="/v1/apiManage/update")
	public Result updateApiManage(@RequestBody ApiManagerParams apiManagerParams){
		if(null==apiManagerParams) {
			return Result.failure("数据格式错误,请检查");
		}
		try {
			apiManageService.updateApiManage(apiManagerParams);
		} catch (ApiVersionManageaveExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiOfTableListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiOfTableOfFieldIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldListIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiRequestParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiResponseParamDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (UpdateFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (AddFieldDuplicatePenNameException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (RequestParamAndUpdateFieldDuplicatePenNameExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (SqlTempletIsNullExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (JSQLParserException e) {
			e.printStackTrace();
			return new Result(false, "Sql语句语法不对,请检查!");
		} catch (ApiRequestParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());	
		} catch (ApiResponseParamDuplicateBySqlException e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiStatusDisableExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		} catch (ApiVersionStatusDisableExceptiuon e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		return new Result(true,"更新成功");
	}
	
	@SuppressWarnings("rawtypes")
	@GetMapping("/list")
    @ApiOperation(value="开发者中心的接口分页获取接口列表",notes ="开发者中心的接口分页获取接口列表")
	//@EnableSystemLog(value="开发者中心获取接口列表",fullPath ="/v1/apiManage/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "name", value = "接口名称",paramType = "query"),
            @ApiImplicitParam(name = "url", value = "接口url",paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用ID",required = true,paramType = "query"),
    })
	
    public Result getApiManageList(@RequestParam(value = "pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "applyId") Integer applyId,
                                   @RequestParam(value = "name",required = false) String name,
                                   @RequestParam(value = "url",required = false) String url){
		if(applyId==null) {
			return new Result(false, "查看应用的的接口,applyId不能为null");
		}
        try{
		PageInfo page = apiManageService.selectPageBySearchParams(pageNum, pageSize, name, url,applyId);
            return new Result(page);
        }catch (Exception e){
            log.error("查询失败", e);
            return new Result(false, "查询失败");
        }
    }
	
	/*@ApiOperation(value="根据请求参数生成sql语句",notes ="根据请求参数生成sql语句")
	public Result generateSqlByParams(@RequestBody SqlParams sqlParams){
		if(null==sqlParams) {
			return Result.failure("数据格式错误,请检查");
		}
		if(sqlParams.getTableName().size()<0||sqlParams.getResponseFieldName().size()<0||sqlParams.getRequestFieldName().size()<0) {
			 return new Result(false, "表名称,请求参数名称,响应参数名称不能为空");
		}
		return null;
	}*/
	/*@GetMapping("/generateFieldBySql")
	@ApiOperation(value="根据编辑的sql语句生成请求字段和响应字段",notes ="根据编辑的sql语句生成请求字段和响应字段")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "sqlStr", value = "编辑的sql语句",required = true,paramType = "query")
})
	public Result generateFieldBySql(@RequestParam String sqlStr){
		if(StringUtil.isEmpty(sqlStr)) {
			return new Result(false, "请求的sql语句不能为空");
		}
		if(sqlParams.getTableName().size()<0||sqlParams.getResponseFieldName().size()<0||sqlParams.getRequestFieldName().size()<0) {
			return new Result(false, "表名称,请求参数名称,响应参数名称不能为空");
		}
		return null;
	}*/
	
	@GetMapping("/details")
    @ApiOperation(value="接口管理修改之前接口详情",notes ="接口管理修改之前接口详情")
	//@EnableSystemLog(value="接口管理修改之前接口详情",fullPath ="/v1/apiManage/details")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "接口id主键",required = true,paramType = "query"),
            @ApiImplicitParam(name = "versionNumber", value = "接口版本号",required = true,paramType = "query")
    })
    public Result getApiManageDetails( @RequestParam(value = "id") Integer id,@RequestParam(value = "versionNumber") String versionNumber){
        try{
        	Map<String, Object> resultMap = apiManageService.getApiManageDetails(id,versionNumber);
            return new Result(resultMap);
        }catch (Exception e){
            log.error("查询接口详情失败", e);
            return new Result(false, "查询接口详情失败");
        }
    }
	@GetMapping("/apiVersionDetails")
	@ApiOperation(value="接口管理点击接口的ulr查看的详情",notes ="接口管理点击接口的ulr查看的详情")
	//@EnableSystemLog(value="接口管理点击接口的ulr查看的详情",fullPath ="/v1/apiManage/apiVersionDetails")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "apiId", value = "接口主键",required = true,paramType = "query")
	})
	public Result apiVersionDetails( @RequestParam(value = "apiId") Integer apiId){
		if(apiId==null) {
			return new Result(false, "接口Id不能为空");
		}
		try{
			Map<String, Object> resultMap = apiManageService.getApiVersiondetails(apiId);
			return new Result(resultMap);
		}catch (Exception e){
			log.error("查询接口详情失败", e);
			return new Result(false, "查询接口详情失败");
		}
	}
	@SuppressWarnings("rawtypes")
	@GetMapping("/verifyPassApiManageList")
    @ApiOperation(value="接口管理获取审核通过的接口列表",notes ="接口管理获取审核通过的接口列表")
	//@EnableSystemLog(value="接口管理获取审核通过的接口列表",fullPath ="/v1/apiManage/verifyPassApiManageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "name", value = "接口名称",paramType = "query"),
            @ApiImplicitParam(name = "apiTypeId", value = "接口类型ID",paramType = "query"),
            @ApiImplicitParam(name = "apiType", value = "接口类型",paramType = "query")
    })
    public Result getVerifyPassApiManagePageList(@RequestParam(value = "pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "name",required = false) String name,
                                   @RequestParam(value = "apiTypeId",required = false) String apiTypeId,
								   @RequestParam(value = "apiType",required = false) String apiType){
        try{
           
		PageInfo page = apiManageService.selectVerifyPassApiManagePageBySearchParams(pageNum, pageSize, name, apiTypeId,apiType);
            return new Result(page);
        }catch (Exception e){
            log.error("查询失败", e);
            return new Result(false, "查询失败");
        }
    }
	/*@GetMapping("/verifyPassApiManagedetails")
    @ApiOperation(value="审核通过的接口详情",notes ="审核通过的接口详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "接口id主键",required = true,paramType = "query")
    })
    public Result getverifyPassApiManagedetails( @RequestParam(value = "id") Integer id){
        try{
        	List<ApiVersionManage> apiVersionManageList=null;
        	Map<String,Object> resultMap=new HashMap<String,Object>();
        	ApiManage apiManage   =  apiManageService.getApiManageDetails(id);
        	if(null!=apiManage) {
        		apiVersionManageList= apiVersionManageService.selectApiVersionManageListByApiId(apiManage.getId());
        	}
        	resultMap.put("apiInfo", apiManage);
        	resultMap.put("apiVersionList", apiVersionManageList);
            return new Result(resultMap);
        }catch (Exception e){
            log.error("审核通过的接口详情失败", e);
            return new Result(false, "审核通过的接口详情详情失败");
        }
    }*/
	@GetMapping("/verifyApiManageList")
    @ApiOperation(value="审核管理->API审核分页列表",notes ="审核管理->API审核分页列表")
	//@EnableSystemLog(value="API审核分页列表",fullPath ="/v1/apiManage/verifyApiManageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "applyName", value = "应用名称",paramType = "query"),
            @ApiImplicitParam(name = "verifyStatus", value = "审核状态:WAIT_VERIFY(等待审核),VERIFY_PASS(审核通过),VERIFY_REJECT(审核驳回),空代表全部",paramType = "query"),
            @ApiImplicitParam(name = "changeType", value = "变动类型:ADD(新增),DELETE(删除),UPDATE(修改),空代表全部",paramType = "query"),
            @ApiImplicitParam(name = "apiName", value = "接口名称",paramType = "query")
    })
    public Result getVerifyApiManagePageList(@RequestParam(value = "pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "applyName",required = false) String applyName,
                                   @RequestParam(value = "verifyStatus",required = false) String verifyStatus,
                                   @RequestParam(value = "changeType",required = false) String changeType,
                                   @RequestParam(value = "apiName",required = false) String apiName){
        try{
		PageInfo page = apiManageService.selectVerifyApiManagePageBySearchParams(pageNum, pageSize, applyName, verifyStatus,changeType,apiName);
            return new Result(page);
        }catch (Exception e){
            log.error("查询失败", e);
            return new Result(false, "查询失败");
        }
        
    }
	@PutMapping("/verifyIngApiManage")
    @ApiOperation(value="审核管理->API审核",notes ="审核管理->API审核")
	@EnableSystemLog(value="API审核",fullPath ="/v1/apiManage/verifyIngApiManage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiVersionId", value = "接口版本的id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "verifyStatus", value = "审核状态:VERIFY_PASS(审核通过),VERIFY_REJECT(审核驳回)",required = true,paramType = "query"),
            @ApiImplicitParam(name = "verifyBy", value = "审核人",required = true,paramType = "query"),
            @ApiImplicitParam(name = "verifyDesc", value = "审核描述",paramType = "query"),
    })
    public Result verifyIngApiManage(@RequestParam(value = "apiVersionId") Integer apiVersionId,
    		                        @RequestParam(value = "verifyStatus") String verifyStatus,
    		                        @RequestParam(value = "verifyBy") String verifyBy,
    		                        @RequestParam(value = "verifyDesc",required = false) String verifyDesc){
        try{
        	apiVersionManageService.verifyIngApiManage(apiVersionId,verifyStatus,verifyBy,verifyDesc);
            return new Result(true,"审核成功");
        }catch (Exception e){
            log.error("审核操作失败", e);
            return new Result(false, "审核操作失败");
        }
    }
	
	/*@GetMapping("/getToken")
	public Result getToken(@RequestParam String appId,@RequestParam String appKey){
		//需要去校验appId和appKey的准确性
		String token=UUID.randomUUID().toString().replace("-", "");
		redisDao.setKeyAndValueTimeout(appId+":token", token,60);
		return new Result(true,"获取成功",token);
	}*/
	
	
	@PutMapping("/enableOrDisableOperation")
	@ApiOperation(value="接口管理列表页面中的启用、禁用操作",notes ="接口管理列表页面中的启用、禁用操作")
	@EnableSystemLog(value="接口启用、禁用操作",fullPath ="/v1/apiManage/enableOrDisableOperation")
	public Result enableOrDisableOperation(@RequestBody ApiEnableParams apiEnableParams) {
			if(apiEnableParams!=null && (apiEnableParams.getApiId()==null||StringUtils.isEmpty(apiEnableParams.getStatus()))){
				return new Result(false, "启用或者禁用操作的时候id,status不能为空");
			}
		try{
			apiManageService.enableOrDisableOperation(apiEnableParams.getApiId(),apiEnableParams.getStatus());
			return new Result(true, "操作成功");
		}catch (Exception e){
			log.error("操作失败", e);
			return new Result(false, "操作失败");
		}
	}
	
	@GetMapping("/verifyApiDetails")
    @ApiOperation(value="审核管理->接口详情",notes ="审核管理->接口详情")
	//@EnableSystemLog(value="接口详情",fullPath ="/v1/apiManage/verifyApiDetails")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiVersionId", value = "接口版本的id",required = true,paramType = "query")
    })
    public Result getVerifyApiDetails(@RequestParam(value = "apiVersionId") Integer apiVersionId){
		if(log.isDebugEnabled()) {
    		log.debug("apiVersionId"+apiVersionId);
    	}
		if(apiVersionId==null) {
			return new Result(false,"apiVersionId不能为空");
		}
        try{
        	ApiManage apiManage=null;
        	List<ApiRequestParam> apiRequestParamList=null;
        	List<ApiResponseParam> responseParamList=null;
        	List<AddField> addFieldList=null;
        	List<UpdateField> updateFieldList=null;
        	Map<String,Object> reaponseMap =new HashMap<>();
        	ApiVersionManage apiVersionManage = apiVersionManageService.selectByPrimaryKey(apiVersionId);
        	if(apiVersionManage!=null) {
        		apiManage = apiManageService.selectApiManageByPrimaryKey(apiVersionManage.getApiId());
        		//从缓存中取出接口调用次数
        		String url = apiManage.getUrl();
				String zsetMember = url.replaceFirst("\\/", "").replace("/", ":");
				double zscore = CacheZSet.zscore(PER_API_VISITCOUNT, zsetMember);
				if(zscore==-1.0) {
					apiManage.setUseTimes(0.0);
				}else {
					
					apiManage.setUseTimes(zscore);
				}
        		apiRequestParamList= apiRequestParamService.selectApiRequestParamListByApiVersionId(apiVersionManage.getId());
        		responseParamList = apiResponseParamService.selectApiResponseParamListByApiVersionId(apiVersionManage.getId());
        		addFieldList= addFieldService.selectAddFieldListByApiVersionId(apiVersionManage.getId());
        		updateFieldList= updateFieldService.selectUpdateFieldListByApiVersionId(apiVersionManage.getId());
        	}
        	reaponseMap.put("changeInfo", apiVersionManage);
        	reaponseMap.put("apiBasicInfo", apiManage);
        	reaponseMap.put("requestParams", apiRequestParamList);
        	reaponseMap.put("responseParams", responseParamList);
        	reaponseMap.put("addFields", addFieldList);
        	reaponseMap.put("updateFields", updateFieldList);
        	return new Result(reaponseMap);
        }catch (Exception e){
            log.error("查询接口详情失败", e);
            return new Result(false, "查询接口详情失败");
        }
    }
	@GetMapping("/validateApiUrl")
	@ApiOperation(value="新增接口的时候需要验证接口的url不能重复",notes ="新增接口的时候需要验证接口的url不能重复")
	//@EnableSystemLog(value="接口url重复验证",fullPath ="/v1/apiManage/validateApiUrl")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "url", value = "接口url",required = true,paramType = "query")
	})
	public Result validateApiUrl( @RequestParam(value = "url") String url){
		if(StringUtils.isEmpty(url)) {
			return new Result(false, "接口url不能为空");
		}
		try{
			StringBuffer sb=new StringBuffer();
			sb.append("/data-api").append(url);
			boolean flag = apiManageService.validateApiUrl(sb.toString());
			return new Result(flag);
		}catch (Exception e){
			log.error("接口url重复校验失败", e);
			return new Result(false, "接口url重复校验失败");
		}
	}
	@GetMapping("/changeRecord")
	@ApiOperation(value="开发者中心接口变动记录",notes ="开发者中心接口变动记录")
	//@EnableSystemLog(value="开发者中心接口变动记录",fullPath ="/v1/apiManage/changeRecord")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "apiId",value ="接口Id",required = true,paramType = "query"),
        @ApiImplicitParam(name = "verifyStatus", value = "审核状态:WAIT_VERIFY(等待审核),VERIFY_PASS(审核通过),VERIFY_REJECT(审核驳回),空代表全部",paramType = "query"),
        @ApiImplicitParam(name = "changeType", value = "变动类型:ADD(新增),DELETE(删除),UPDATE(修改),空代表全部",paramType = "query"),
	})
	public Result selectChangeRecordByApiId(@RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value="apiId") Integer apiId,
            @RequestParam(value = "verifyStatus",required = false) String verifyStatus,
            @RequestParam(value = "changeType",required = false) String changeType) {
		if(null==apiId) {
			return new Result(false, "查看接口的变动记录apiId不能为空");
		}
	   try{
		  PageInfo page = apiManageService.selectChangeRecordByApiId(pageNum, pageSize, apiId, verifyStatus, changeType);
	          return new Result(page);
	       }catch (Exception e){
	            log.error("查询失败", e);
	            return new Result(false, "查询失败");
	      }
	}
	
	@PutMapping("/deleteApply")
    @ApiOperation(value="开发者中心删除接口申请",notes ="开发者中心删除接口申请")
	@EnableSystemLog(value="开发者中心删除接口申请",fullPath ="/v1/apiManage/deleteApply")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "apiVersionId", value = "接口版本的id主键",required = true,paramType = "query"),
    })
    public Result deleteApply( @RequestParam(value = "apiVersionId") Integer apiVersionId){
		if(null==apiVersionId) {
			return new Result(false, "开发者发出接口删除申请的apiVersionId:"+apiVersionId);
		}
		if(log.isInfoEnabled()) {
			log.info("开发者中心发起接口删除申请的apiVersionId:"+apiVersionId);
		}
        try{
           apiManageService.deleteApply(apiVersionId);
            return new Result(true,"删除申请成功");
        }catch (Exception e){
            log.error("开发者中心发起接口删除申请失败", e);
            return new Result(false, "开发者中心发起接口删除申请失败");
        }
        
    }
	
	@GetMapping("/dataApiShowList")
	@ApiOperation(value="门户数据接口显示列表",notes ="门户数据接口显示列表")
	//@EnableSystemLog(value="门户数据接口显示列表",fullPath ="/v1/apiManage/dataApiShowList")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
        @ApiImplicitParam(name = "apiTypeId",value ="接口分类目录Id",paramType = "query"),
        @ApiImplicitParam(name = "useOrder", value = "asc(升序),desc(降序)",paramType = "query"),
        @ApiImplicitParam(name = "updateTimeOrder", value = "asc(升序),desc(降序),默认降序",paramType = "query"),
    })
	public Result selectDataApiShowList(@RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value="apiTypeId" ,required = false) Integer apiTypeId,
            @RequestParam(value = "useOrder",required = false) String useOrder,
            @RequestParam(value = "updateTimeOrder",required = false) String updateTimeOrder) {
	   try{
            if(StringUtils.isNotEmpty(useOrder)){
				if(RegexUtils.checkSpecialCharacters(useOrder)){
					return new Result(false, "包含特殊字符");
				}
			}
		   if(StringUtils.isNotEmpty(updateTimeOrder)){
			   if(RegexUtils.checkSpecialCharacters(updateTimeOrder)){
				   return new Result(false, "包含特殊字符");
			   }
		   }
		   if(StringUtils.isNotEmpty(useOrder) && StringUtils.isNotEmpty(updateTimeOrder)){
				   return new Result(false, "排序字段不能同时排序");
		   }
		   Integer count =  apiManageService.selectDataApiShowListCount(apiTypeId);
		   Integer pageCount = (count + pageSize - 1)/pageSize +1;
		   if(pageNum>pageCount){
			   log.error("页数不能超过总页数,总页数为:"+pageCount );
			   return new Result(false,"页数不能超过总页数,总页数为:"+pageCount);
		   }
		  PageInfo page = apiManageService.selectDataApiShowList(pageNum, pageSize, apiTypeId, useOrder, updateTimeOrder);
	          return new Result(page);
	       }catch (Exception e){
	            log.error("查询门户接口数据失败", e);
	            return new Result(false, "查询门户接口数据失败");
	      }
	}

	@GetMapping("/getApiByTableId")
	@ApiOperation(value="查询表关联的接口列表",notes ="查询表关联的接口列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
			@ApiImplicitParam(name = "tableId",value ="表id",required = true,paramType = "query")
	})
	public Result  getApiByTableId(@RequestParam(value = "pageNum") Integer pageNum,
								   @RequestParam(value = "pageSize") Integer pageSize,
								   @RequestParam(value="tableId" ) Integer tableId){
		try{
			PageInfo page = apiManageService.getApiByTableId(pageNum, pageSize, tableId);
			return new Result(page);
		}catch (Exception e){
			log.error("查询表关联的接口列表失败", e);
			return new Result(false, "查询表关联的接口列表失败");
		}
		
	}

	@GetMapping("/getApiVersionByTableId")
	@ApiOperation(value="获取表关联的接口版本列表",notes ="获取表关联的接口版本列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
			@ApiImplicitParam(name = "tableId",value ="表id",required = true,paramType = "query")
	})
	public Result  getApiVersionByTableId(@RequestParam(value = "pageNum") Integer pageNum,
								   @RequestParam(value = "pageSize") Integer pageSize,
								   @RequestParam(value="tableId" ) Integer tableId){
		try{
			PageInfo page = apiManageService.getApiVersionByTableId(pageNum, pageSize, tableId);
			return new Result(page);
		}catch (Exception e){
			log.error("获取表关联的接口版本列表失败", e);
			return new Result(false, "获取表关联的接口版本列表失败");
		}

	}

	@GetMapping("/searchApiListByName")
    @ApiOperation(value="首页根据接口名称搜索接口列表",notes ="首页根据接口名称搜索接口列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "name", value = "接口名称",paramType = "query")
    })
    public Result searchApiListByName(@RequestParam(value = "pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "name",required = false) String name){
		try{
		PageInfo page = apiManageService.searchApiListByName(pageNum, pageSize,name);
            return new Result(page);
        }catch (Exception e){
            log.error("查询失败", e);
            return new Result(false, "查询失败");
        }
        
    }


	/*@GetMapping("/getApiByTableId/{tableId}")
	@ApiOperation(value="查询表关联的接口列表",notes ="查询表关联的接口列表")
	//@EnableSystemLog(value="接口管理获取审核通过的接口列表",fullPath ="/v1/apiManage/verifyPassApiManageList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tableId",value = "表id",required = true,paramType = "path"),
			@ApiImplicitParam(name = "pageNum",value = "当前页数",required = true,paramType = "query"),
			@ApiImplicitParam(name = "pageSize",value = "显示行数",required = true,paramType = "query")
	})
	public Result getApiByTableId(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize,@PathVariable("tableId") Integer tableId){
		try{
			PageInfo page = apiManageService.getApiByTableId(pageNum, pageSize,tableId);
			return new Result(page);
		}catch (Exception e){
			log.error("查询表关联的接口列表失败", e);
			return new Result(false, "查询表关联的接口列表失败");
		}
	}*/
}
